package org.kl.bf.service.basic;

import java.util.List;
import java.util.Map;

import org.kl.bf.entity.basic.RunAsConfig;
import org.kl.bf.entity.basic.User;
import org.kl.bf.repository.basic.RunAsConfigDao;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.utils.Collections3;

/**
 * @Author hqm
 * @CreateTime 2014年6月17日 下午8:52:08
 */
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class RunAsConfigService {

	private RunAsConfigDao runAsConfigDao;

	@Autowired
	public void setRunAsConfigDao(RunAsConfigDao runAsConfigDao) {
		this.runAsConfigDao = runAsConfigDao;
	}

	public RunAsConfig getRunAsConfig(String guid) {
		return runAsConfigDao.findOne(guid);
	}

	public void saveRunAsConfig(RunAsConfig entity) {
		runAsConfigDao.save(entity);
	}

	/**
	 * 授权
	 * 
	 * @param fromUserId
	 * @param toUserId
	 */
	public void grantRunAsConfig(String fromUserId, String toUserId) {
		RunAsConfig runAsConfig = new RunAsConfig();
		runAsConfig.setFromUser(new User(fromUserId));
		runAsConfig.setToUser(new User(toUserId));
		runAsConfig.setInUse(1);
		runAsConfigDao.save(runAsConfig);
	}

	/**
	 * 取消授权
	 * 
	 * @param fromUserId
	 * @param toUserId
	 */
	public void revokeRunAsConfig(String fromUserId, String toUserId) {
		List<RunAsConfig> result = runAsConfigDao.findByFromUserIdAndToUserIdAndInUse(fromUserId, toUserId, 1);
		for (RunAsConfig rac : result) {
			rac.setInUse(0);
			runAsConfigDao.save(rac);
		}
	}

	/**
	 * 取消授权
	 * 
	 * @param fromUserId
	 * @param toUserId
	 */
	public void revokeRunAsConfig(String id) {
		RunAsConfig rac = runAsConfigDao.findOne(id);
		rac.setInUse(0);
		runAsConfigDao.save(rac);
	}

	/**
	 * 是否存在授权
	 * 
	 * @param fromUserId
	 * @param toUserId
	 */
	public boolean exists(String fromUserId, String toUserId) {
		List<RunAsConfig> result = runAsConfigDao.findByFromUserIdAndToUserIdAndInUse(fromUserId, toUserId, 1);
		if (Collections3.isNotEmpty(result)) {
			return true;
		} else {
			return false;
		}
	}

	public void deleteRunAsConfig(String guid) {
		runAsConfigDao.delete(guid);
	}

	public List<RunAsConfig> getAllRunAsConfig() {
		return runAsConfigDao.findAll();
	}

	public Page<RunAsConfig> getRunAsConfig(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<RunAsConfig> spec = buildSpecification(searchParams);

		return runAsConfigDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<RunAsConfig> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<RunAsConfig> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), RunAsConfig.class);
		return spec;
	}

}
